from django.shortcuts import render, redirect
from django.views import View
from django import http
import json


# Create your views here.

# 1、分别利用查询字符串、表单、json字符串传参的方式，由postman向后端传递姓名、年龄、邮箱三个参数并在控制台上打印。
# --------------------提取查询字符串参数----------------------------
# 1.创建视图
# http://127.0.0.1:8000/querystring/?name=query&age=99&email=query@126.com
# 2.注册路由
# 3.提取参数，运行并打印在控制台上
class QueryStringView(View):
	def get(self, request):
		name = request.GET.get('name')
		age = request.GET.get('age')
		email = request.GET.get('email')
		print(name, age, email)
		return http.HttpResponse('GRT-提取查询字符串参数')

	def post(self, request):
		name = request.GET.get('name')
		age = request.GET.get('age')
		email = request.GET.get('email')
		print(name, age, email)
		return http.HttpResponse('POST-提取查询字符串参数')


# --------------------提取请求体body表单参数----------------------------
# 1.创建类视图
# http://127.0.0.1:8000/formbody/
# 2.注册路由
# 3.提取数据
class FormBodyView(View):
	def post(self, request):
		name = request.POST.get('name')
		age = request.POST.get('age')
		email = request.POST.get('email')
		print(name, age, email)
		return http.HttpResponse('POST-提取请求体body表单参数')


# --------------------提取请求体json参数----------------------------
# 1.创建类视图
# http://127.0.0.1:8000/jsonbody/
# 2.注册路由
# 3.提取数据，需要导入JSON模块
class JsonBodyView(View):
	def post(self, request):
		json_data = request.body
		json_dict = json.loads(json_data)

		name = json_dict.get('name')
		age = json_dict.get('age')
		email = json_dict.get('email')

		print(name, age, email)
		return http.HttpResponse('POST-提取请求体body JSON参数')


# 2、用路径传参的方式完成上题功能，要求定义路由转换器，限定邮箱必须
# 为126/163/qq，否则报错。
# --------------------提取url路径参数----------------------------

# http://127.0.0.1:8000/urlpath/url/22/url.@126.com/
class UrlPathView(View):

	def get(self, request, name, age, email):
		print(name, age, email)
		return http.HttpResponse(f'GET-姓名:{name} 年龄:{age} 邮箱:{email}')


# -----------------------------HttpResponse响应信息-----------------------------------------------
# 1.创建视图
# http://127.0.0.1:8000/reponse/
# 2.注册路由
# 3.返回响应

# 1.创建视图，定义请求方法
# 2.注册路由
# 3.使用request.body接收JSON字符串
# 4.http.HttpResponse(响应JSON字符串)
class JsonResponseView(View):

	def post(self, request):
		# 		request_data={
		#     "name": "json",
		#     "age": "111",
		#     "email": "json@126.com"
		# }
		request_data = request.body
		# print(type(request_data))
		a = request_data.decode()
		b = json.loads(a)
		# print(b)
		# print('a',type(a))
		# print('b',type(b))
		# request_dict = json.loads(request_data)
		return http.JsonResponse(b)


# -----------------------------redirect重定向--------------------------
class LoginrequestView(View):
# http://127.0.0.1:8000/login/
	def get(self,request):
		#重定向到下面视图响应图片
		return redirect('demo_work:image')

	# ----------------------响应图片--------------------------


import os
class ImageResponseView(View):
	# 	# http://127.0.0.1:8000/images/
	def get(self, request):
		# 1, str2, str3, str4, str5, str6, str7
		print(os.getcwd())
		path = 'C:/Users/Flydream/Desktop/大杂烩/Image/image.jpg'
		response_data = open(path, 'rb').read()
		# print(response_data)
		return http.HttpResponse(response_data, content_type="image/jpg")
